/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import org.opu.vcforms.bean.ApplicationData;
import org.opu.vcforms.bean.EventBus;
import org.opu.vcforms.bean.db.ConfigPath;
import org.opu.vcforms.io.FileIO;
import org.opu.vcforms.io.Logger;
import org.opu.vcforms.resource.JProperties;
import org.opu.vcforms.resource.ResourceHandle;
import org.opu.vcforms.resource.ResourceHandle.IconType;

/**
 *
 * @author yura
 */
public class ApplicationView extends JFrame {
    public static final String LOCATION = "location";
    public static final String MAXIMIZE = "maximize";
    public static final String SIZE = "size";

    protected ApplicationContent content;
    protected ApplicationMenuBar menubar;
    protected ApplicationToolbar toolbar;

    private JProperties properties;
    protected String appTtl;

    private ApplicationData data;
    private EventBus events;

    public ApplicationView(ApplicationData data, EventBus events) {
        this.data = data;
        this.events = events;
        appTtl = ResourceHandle.getProperty(ResourceHandle.APPLICATION_TITLE);
        properties = new JProperties();
        initFrame();

        DefaultApplicationViewI18n i18n = new DefaultApplicationViewI18n();

        content = new ApplicationContent(data, events, i18n);
        menubar = new ApplicationMenuBar(events, i18n);
        toolbar = new ApplicationToolbar(events);

        Container contentPane = getContentPane();
        contentPane.add(content);
        contentPane.add(toolbar, BorderLayout.NORTH);
        setJMenuBar(menubar);
    }

    private void initFrame(){
        try {
            File file = ConfigPath.getInstance().getFrameProp();
            FileIO.load(file, properties);
        } catch (IOException ex) {
            Logger.warning(getClass().getName() + ": When load properties - " + ex);
        }

        setSize(properties.getDimension(SIZE, new Dimension(540, 415)));
        Point point = properties.getPoint(LOCATION);
        if (point != null) {
            setLocation(point);
        } else {
            setLocationRelativeTo(null);
        }

        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setTitle(appTtl);
        setIconImage(ResourceHandle.getImage("Statement.png", IconType.medium));

        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                events.getExitAction().actionPerformed(null);
            }

        });

        events.listeners.add(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                store();
            }
        });
    }

    private void store(){
        File file = ConfigPath.getInstance().getFrameProp();

        properties.setPoint(LOCATION, getLocation());
        properties.setDimension(SIZE, getSize());
        properties.setBoolean(MAXIMIZE, isMaximumSizeSet());

        try {
            FileIO.store(file, properties, "window");
        } catch (IOException ex) {
            Logger.warning(getClass().getName() + ": When store properties - " + ex);
        }
    }
    
}
